/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builders;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.annotation.util.CriteriaUtilities;
import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.FieldComponent;
import org.balisunrise.common.FilterField;

/**
 *
 * @author glauco
 */
public class FilterFieldBuilder 
        implements org.balisunrise.builder.FilterFieldBuilder{

    private static final String MESSAGE = "Erro ao construir uma FilterField.\n";

    @Override
    public FilterField buildFilterField(Field field, String alias)
            throws BuilderException {

        if(field == null){
            throw new BuilderException(MESSAGE +
                    "field não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        try{
            return buildFilterField(CriteriaUtilities.create(field, alias));
        }catch(AnnotationsUtilitiesException auex){
            throw new BuilderException(MESSAGE +
                    auex.getMessage(), auex.getCause());
        }
    }

    @Override
    public FilterField buildFilterField(CriteriaUtilities cu)
            throws BuilderException {
        
        if(cu == null){
            throw new BuilderException(MESSAGE +
                    "criteriaUtilities não pode ser nulo.");
        }

        switch(cu.type){
            case CriteriaUtilities.DEFINITION:
                return buildDefinition(cu);
            case CriteriaUtilities.EMBEDDED:
                return buildEmbedded(cu);
            case CriteriaUtilities.SELECTION:
                return buildSelection(cu);
        }

        throw new BuilderException(MESSAGE +
                    "Atributo TYPE do fieldUtilities invalido.");
    }

    private FilterField buildDefinition(CriteriaUtilities cu)
            throws BuilderException{

        org.balisunrise.swing.commons.FilterField ff =
                new org.balisunrise.swing.commons.FilterField();

        ff.setAlias(cu.alias);
        ff.setEntityClass(cu.field.getDeclaringClass());
        ff.setFieldClass(cu.field.getType());
        ff.setFieldName(cu.field.getName());
        ff.setFields(FieldComponentsBuilder.buildFilterFieldComponents(cu));
        ff.setJumpToNextRow(Boolean.TRUE);
        ff.setEmptyForAll(cu.emptyForAll);
        ff.setFilterType(cu.filterType);
        ff.setName(NameUtil.buildName(ff));

        for (FieldComponent fc : ff.getFieldComponents()) {
            fc.setOwner(ff);
        }

        return ff;
    }

    private FilterField buildEmbedded(CriteriaUtilities cu)
            throws BuilderException{
       
        List<FilterField> list = new ArrayList<FilterField>();
        
        // para cada campo emcapsulado
        for (CriteriaUtilities cr : cu.embedded) {
            // cria novos campos
            FilterField ff = buildDefinition(cr);
            // adiciona pre-name
            ff.getCriterion().addPreName(cu.field.getName());
            // adiciona a lista final
            list.add(ff);
        }
        
        // retorna um EmbeddedFilterField
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private FilterField buildSelection(CriteriaUtilities cu)
            throws BuilderException{
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
